#!/bin/csh

####################################################################
#
# VZEST deck
#
####################################################################

setenv VZLAMBDA  0.1
setenv VLAMBDA   3.0
setenv V0LAMBDA  1.0
setenv V1LAMBDA  0.0
setenv ZLAMBDA   8.0
setenv NITERATIONS 40
setenv XUPMAX    100.0
setenv XDNMAX    -100.0

setenv NPICKS    2    
setenv IOFFSET1  0
setenv IOFFSET2  5
setenv SORT1     0.0
setenv SORT2     1500.0
setenv NSORT     5
setenv ISEED     404
setenv TOPCMEAN  1700
setenv BASECMEAN 2800

setenv NXRT      141
setenv NYRT      171
setenv DXRT      100.0
setenv DYRT      100.0
setenv NXRTBEG   1
setenv NXRTEND   141
setenv NYRTBEG   1
setenv NYRTEND   171

setenv NXHT      $NXRTEND-$NXRTBEG+1
setenv NYHT      $NYRTEND-$NYRTBEG+1

setenv IXDISP    50
setenv IYDISP    80

setenv PRECLT    16384

setenv W_EPS     0.1
setenv AZIMUTH   0.0

setenv SGIWORKDIR .
setenv WORKDIR .

# INPUT files (if TOPC0=0 TOPC0MIGFILE will be used)
setenv TOPC 0
setenv TOPCMIGFILE $SGIWORKDIR/c02.intp.grid

# INPUT files (if TOPCG0=0 TOPCG0MIGFILE will be used)
setenv TOPCG 0.0001
setenv TOPCGMIGFILE $SGIWORKDIR/

setenv TOPZGRIDFILE $SGIWORKDIR/WB1.grid
setenv BASECMIGFILE $SGIWORKDIR/ttvelsource.grid
setenv BASEZGRIDFILE $SGIWORKDIR/top_salt3.intp.grid

setenv FNAME01 $SGIWORKDIR/offset$IOFFSET1.intp.grid
setenv FNAME02 $SGIWORKDIR/offset$IOFFSET2.intp.grid

# OUTPUT files
setenv DVXFILE $SGIWORKDIR/dvx.grid
setenv DVYFILE $SGIWORKDIR/dvy.grid
setenv DVZFILE $SGIWORKDIR/dvz.grid
setenv C0NEWFILE $SGIWORKDIR/c0new.grid
setenv G0FILE $SGIWORKDIR/g0file.sm.grid
setenv G1FILE $SGIWORKDIR/g1file.sm.grid
setenv G2FILE $SGIWORKDIR/g2file.sm.grid
setenv G3FILE $SGIWORKDIR/g3file.sm.grid

#
echo 'Defination done'
#
/bin/rm -f CARDDATA
/bin/rm -f icolandrow.m

echo 'CARDDATA deleted'
#
cat > CARDDATA << EOF

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c This is the global parms for VZEST, The 3-D migration velocity analysis.
c
c This file was automatically generated by the MVA deck. Do not modify this 
c file.  If you want to modify parameters, modify the MVA deck file.  Thanks.
c
c The following paragraph will be inserted in the source code (vzest.f) while 
c being compiled. Failure in complying fortran rules will lead to compiling 
c errors/warnings.
c  
c NPICKS is the parameter used by VEST3D.  It is the number of offsets that 
c the user has picked up for migration velocity analysis.  That is also the
c number of misfit grid files one has saved after running 4dv.
        integer npicks
        parameter (npicks=$NPICKS)          !obtained from 4dv

c nxrt is the number of reconstructed imaged points in x direction
c nyrt is the number of reconstructed imaged points in y direction
        integer nxrt,nyrt
        parameter (nxrt=$NXRT)
        parameter (nyrt=$NYRT)

        integer nxht,nyht
        parameter (nxht=$NXHT)
        parameter (nyht=$NYHT)

        integer nmva
        parameter (nmva=nxht*nyht)

	real vlambda
	parameter (vlambda=$VLAMBDA)

	real v0lambda
	parameter (v0lambda=$V0LAMBDA)

	real v1lambda
	parameter (v1lambda=$V1LAMBDA)

	real vzlambda
	parameter (vzlambda=$VZLAMBDA)

	real zlambda
	parameter (zlambda=$ZLAMBDA)

	integer niterations
	parameter (niterations=$NITERATIONS)

        real topcmean
	parameter (topcmean=$TOPCMEAN)

        real basecmean
	parameter (basecmean=$BASECMEAN)

	real xupmaxconst
        parameter (xupmaxconst=$XUPMAX)
	real xdnmaxconst
        parameter (xdnmaxconst=$XDNMAX)

	real dxrt,dyrt
        parameter (dxrt=$DXRT)
        parameter (dyrt=$DYRT)

	integer ixdisp,iydisp
	parameter (ixdisp=$IXDISP)
	parameter (iydisp=$IYDISP)

c iseed is the index number of the imaged point defined by the depth grid
        integer iseed
        parameter (iseed=$ISEED)

c isort defines which offsets one has chosen to apply VZEST
	integer isort($NPICKS)
	data isort/$IOFFSET1,$IOFFSET2/

c The following parameters are used by vest3d, just copy them
	integer nsort
	parameter (nsort=$NSORT)
	real sort1
	parameter (sort1=$SORT1)
	real sort2
	parameter (sort2=$SORT2)

c the filenames for the misfit files.  Fill in these
        character*120 fnames($NPICKS) 
        data fnames(1)/'$FNAME01'/
        data fnames(2)/'$FNAME02'/

c the velocity file at the imaging grid. Copy from your SGI work directory
        character*120 topcmigfile
        data topcmigfile/'$TOPCMIGFILE'/
c we must set up
        real topc 
	data topc/$TOPC/

c the velocity gradient file at the imaging grid.
        character*120 topcgmigfile
        data topcgmigfile/'$TOPCGMIGFILE'/
c we must set up
        real topcg
	data topcg/$TOPCG/

c the imaging grid. Copy from your SGI work directory
        character*120 topzgridfile
        data topzgridfile/'$TOPZGRIDFILE'/

c the velocity file at the imaging grid. Copy from your SGI work directory
        character*120 basecmigfile
        data basecmigfile/'$BASECMIGFILE'/

c the imaging grid. Copy from your SGI work directory
        character*120 basezgridfile
        data basezgridfile/'$BASEZGRIDFILE'/

c output files
        character*120 dvxfile
        data dvxfile/'$DVXFILE'/
        character*120 dvyfile
        data dvyfile/'$DVYFILE'/
        character*120 dvzfile
        data dvzfile/'$DVZFILE'/
        character*120 c0newfile
        data c0newfile/'$C0NEWFILE'/
	character*120 g0file
	data g0file/'$G0FILE'/
	character*120 g1file
	data g1file/'$G1FILE'/
	character*120 g2file
	data g2file/'$G2FILE'/
	character*120 g3file
	data g3file/'$G3FILE'/

        integer nxrtbeg
        parameter (nxrtbeg=$NXRTBEG)         
        integer nxrtend
        parameter (nxrtend=$NXRTEND)
        integer nyrtbeg
        parameter (nyrtbeg=$NYRTBEG)         
        integer nyrtend
        parameter (nyrtend=$NYRTEND)

	real w_eps
	data w_eps/$W_EPS/
	real azimuth
	data azimuth/$AZIMUTH/
EOF
#
echo 'file written'
#
mv CARDDATA vzest_global.parms
#
echo "*******************************************************************"
echo "*                     COMPILING					"
echo "*******************************************************************"
#
/bin/rm vzest
#
make -f $HOME/src/vzest/Makefile
#
echo "*******************************************************************"
echo "*                        VZEST					"
echo "*******************************************************************"
#
vzest
#
echo ""
echo " ********************************************* "
echo " *                                           * "
echo " *            CONGRATULATIONS!               * "
echo " *                                           * "
echo " ********************************************* "
echo ""
